package com.lin.missyou.api.v1;

import com.lin.missyou.core.interceptors.ScopeLevel;
import com.lin.missyou.lib.LinWxNotify;
import com.lin.missyou.service.WxPaymentService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Map;

/**
 * @author gcq
 * @Create 2021-11-19
 */
@RestController
@RequestMapping("payment")
public class PaymentController {

    @Autowired
    private WxPaymentService wxPaymentService;


    /**
     * 订单过期
     *      1、历史订单，最后几秒提交
     *      2、PostMan 发起支付 Button 隐藏
     *      3、前端请求不可信
     * @param oid
     * @return
     */
    @ScopeLevel
    @RequestMapping("/{oid}")
    public Map<String, String> preOrder(@PathVariable Long oid)
    {
        Map<String, String> miniPayParams = wxPaymentService.preOrder(oid);
        return miniPayParams;
    }

    /**
     * 微信支付完成后 回调
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/wx/notify")
    public String payCallBack(HttpServletRequest request,
                              HttpServletResponse response)
    {
        InputStream s;
        try {
            s = request.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
            return LinWxNotify.fail();
        }
        return LinWxNotify.readNotify(s);
    }
}